package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import modal.BidModal;

public class BidsDAO extends BaseDAO{
	
	public ArrayList<BidModal> getBidsOfProductbyId(int productId) throws Exception{
		String query= "SELECT * FROM `bids` WHERE `ProductID`= "+productId+" AND `Status` = 0";
		ResultSet rs =execQuery(query);
		BidModal bid=null;
		ArrayList<BidModal> bids= new ArrayList<BidModal>(); 
		while (rs.next()){	
			bid= new BidModal(rs.getInt("Price"), rs.getInt("Status"), rs.getInt("UserID"), rs.getInt("ProductID"), rs.getInt("BidID"), rs.getString("BidTime"));
			bids.add(bid);
		}
		return bids;
	}
	
	public ArrayList<BidModal> getOffersOfProductbyId(int productId) throws Exception{
		String query= "SELECT * FROM `bids` WHERE `ProductID`= "+productId+" AND `Status` = 1";
		ResultSet rs =execQuery(query);
		BidModal bid=null;
		ArrayList<BidModal> bids= new ArrayList<BidModal>(); 
		while (rs.next()){	
			bid= new BidModal(rs.getInt("Price"), rs.getInt("Status"), rs.getInt("UserID"), rs.getInt("ProductID"), rs.getInt("BidID"), rs.getString("BidTime"));
			bids.add(bid);
		}
		return bids;	
	}
	
	public ArrayList<BidModal> getBidsOfUserbyId(int userId) throws Exception{
		String query= "SELECT * FROM `bids` WHERE `UserID`= "+userId+" AND `Status` = 0";
		ResultSet rs =execQuery(query);
		BidModal bid=null;
		ArrayList<BidModal> bids= new ArrayList<BidModal>(); 
		while (rs.next()){	
			bid= new BidModal(rs.getInt("Price"), rs.getInt("Status"), rs.getInt("UserID"), rs.getInt("ProductID"), rs.getInt("BidID"), rs.getString("BidTime"));
			bids.add(bid);
		}
		return bids;	
		}
	
	public int getHighestPricebyProductId(int productId) throws Exception{
		String query="SELECT MAX(Price) FROM `bids` WHERE `ProductID`="+ productId +" AND `Status`= 0";
		ResultSet rs = execQuery(query);
		int a=0; 
		if(rs.next()){
		 a = rs.getInt(1);
		}
		return a;
	}
	
	public void AddBid(int productId, int userId, int price, int status) throws Exception{
		String query = "INSERT INTO `bids`(`ProductID`, `UserID`, `Price`, `Status`, `BidTime`) VALUES ("
				+ productId
				+ ","
				+ userId
				+ ","
				+ price
				+ ","
				+ status
				+ ","
				+ "NOW())";
		execQuery(query);
	}
		//status = 0 i want to buy 
		//status = 1 i want to sell 

	public int getLowestOfferbyProductId(int productId) throws Exception {
		String query="SELECT MIN(Price) FROM `bids` WHERE `ProductID`="
				+ productId
				+ " AND Status=0";
		ResultSet rs =execQuery(query);
		int a=0; 
		if(rs.next()){
		 a = rs.getInt(1);
		}
		return a;
	}

	
	public Set<Integer> getProductIdOfActiveBidsToBuy(int userId) throws Exception{
		String query= "SELECT `ProductId` FROM `bids` WHERE `UserID`= "+userId+" AND `Status` = 0";
		ResultSet rs =execQuery(query);
		Set<Integer> prod = new HashSet<Integer>();
		while (rs.next()){	
			prod.add(rs.getInt(1));
		}
		return prod;	
		}
	
	
	
	
	
	
}
